/*
 * @(#)JdbcResultSet.java
 *
 * Copyright 2001 Orlin Tomov. All Rights Reserved.
 * 
 */

package dbu.jdbc;

import java.sql.SQLException;

import dbu.Data;
import dbu.jdbc.facade.JdbcResultSetImpl;

/**
 * A class ...
 *
 * @version     1.00, 01/09/01
 * @author      Orlin Tomov
 */

public final class JdbcResultSet extends JdbcResultSetImpl {

	private Data data;
	private int index = -1;

	// TODO not public
	public JdbcResultSet(Data data) throws SQLException {
		this.data = data;
	}

	/** Moves the cursor to the front of this ResultSet object, just before the first row. */
	@Override
	public void beforeFirst() throws SQLException {
		index = -1;
	}

	/** Indicates whether the cursor is before the first row in this ResultSet object. */
	@Override
	public boolean isBeforeFirst() throws SQLException {
		return index < 0;
	}

	/** Moves the cursor to the first row in this ResultSet object. */
	@Override
	public boolean first() throws SQLException {
		index = 0;
		return isFirst();
	}

	/** Indicates whether the cursor is on the first row of this ResultSet object. */
	@Override
	public boolean isFirst() throws SQLException {
		return (index == 0) && (index < data.size());
	}

	/** Moves the cursor to the last row in this ResultSet object. */
	@Override
	public boolean last() throws SQLException {
		index = data.size() - 1;
		return isLast();
	}

	/** Indicates whether the cursor is on the last row of this ResultSet object. */
	@Override
	public boolean isLast() throws SQLException {
		return index == data.size() - 1;
	}

	/**  Moves the cursor to the end of this ResultSet object, just after the last row. */
	@Override
	public void afterLast() throws SQLException {
		index = data.size();
	}

	/** Indicates whether the cursor is after the last row in this ResultSet object. */
	@Override
	public boolean isAfterLast() throws SQLException {
		return index >= data.size();
	}

	/** Moves the cursor to the previous row in this ResultSet object. */
	@Override
	public boolean previous() throws SQLException {
		index--;
		return !isBeforeFirst();
	}

	/** Moves the cursor down one row from its current position. */
	@Override
	public boolean next() throws SQLException {
		index++;
		return !isAfterLast();
	}

	/** Gets the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. */
	@Override
	public String getString(int columnIndex) throws SQLException {
		return data.getString(index, columnIndex);
	}

	/** Retrieves the number, types and properties of this ResultSet object's columns. */
	@Override
	public JdbcResultSetMetaData getMetaData() throws SQLException {
		return data.getMeta();
	}
}
